import {
	storeToRefs
} from 'pinia';
import initApi from '@/apis/init.js';
import {
	useUserStore
} from '@/store/user.js';
const {
	userInfo
} = storeToRefs(useUserStore());
export function getALPhoneNumber() {
	console.log('阿里巴巴');
	return new Promise((resolve, reject) => {
		my.getAuthCode({
			scopes: 'auth_user',
			success: res => {
				console.log(res, 'resfirst');
				const authCode = res.authCode;
				// 在服务端获取用户信息
				initApi.getApplyLogin({
					code: authCode
				}).then(resp => {
					console.log(resp, 'resp', res.code);
					if (resp.code == 20000) {

						uni.setStorageSync('token', resp.data.accessToken);
						userInfo.value.token = resp.data.accessToken
						console.log("获取 accessToken --->>");


						initApi.getUserInfo().then((resUserInfo) => {
							console.log('支付宝登陆数据', resUserInfo, resUserInfo.code == 20000);
							if (resUserInfo.code == 20000) {
								userInfo.value.mobile = resUserInfo.data.mobile;
								userInfo.value.username = resUserInfo.data.username;
								userInfo.value.info = resUserInfo.data;
								console.log(userInfo.value, '		console.log(userInfo.value);');

								uni.setStorageSync('info', JSON.stringify(resUserInfo.data));
								resolve(resUserInfo);
							} else {
								return uni.utils.toast(resUserInfo.message);
							}
						});

					} else {
						return uni.utils.toast(resp.message);
					}
				})
			},
			fail: err => {
				console.log('my.getAuthCode 调用失败', err)
			}
		});
	})

}

export function getWXphoneNumber(alle) {
	console.log('微信小程序');
	return new Promise((resolve,reject)=>{
		wx.login({
			success(res) {
				console.log(res, 'res');
				const arr = {
					appId: 'wxd288833c4af82e86',
					code: res.code,
					encryptedData: null,
					iv: null
				};
				if (res.code) {
					initApi.getWXLogin(arr).then((res) => {
						if (res.code == 20000) {
							uni.setStorageSync('token', res.data.accessToken);
							userInfo.value.token = res.data.accessToken
		
							initApi.getUserInfo().then((res1) => {
								console.log('微信登陆数据', res1);
								if (res1.code == 20000) {
									if(res1.data.mobile){
										userInfo.value.mobile = res1.data.mobile;
										userInfo.value.username = res1.data.username;
										userInfo.value.info = res1.data;
										uni.setStorageSync('info', JSON.stringify(res1.data));
										resolve(res1.data)
									}else{
										// wxGetSecondPhone(alle)
										const {
											detail: { encryptedData, iv, code }
										} = alle;
										const params = {
											appId: 'wxd288833c4af82e86',
											code
										};
										initApi.getWXAuthor(params).then((resSecond) => {
											console.log(resSecond,'自己手动获取手机号');
											if (resSecond.code == 20000) {
												if (resSecond.data.accessToken) {
													uni.setStorageSync('token', resSecond.data.accessToken);
													userInfo.value.token = resSecond.data.accessToken
													console.log("accessToken 替换");
												}
												userInfo.value.mobile = resSecond.data.mobile
												console.log('20000', resSecond.data);
												userInfo.value.info = resSecond.data;
												uni.setStorageSync('info', JSON.stringify(resSecond.data));
												// 下单
												resolve(resSecond)
											}
										});
									}
								
								} else {
									return uni.utils.toast(res1.message);
								}
							});
		
						} else {
							return uni.utils.toast(res.message);
						}
					});
				} else {
					console.log('登录失败！' + res.errMsg);
				}
			}
		});
	})

}
/* 微信未获取到手机号 */
export function wxGetSecondPhone(e){
	console.log(e, 'eeeee========');
	const {
		detail: { encryptedData, iv, code }
	} = e;
	const params = {
		appId: 'wxd288833c4af82e86',
		code
	};
	initApi.getWXAuthor(params).then((res) => {
		console.log(res,'自己手动获取手机号');
		if (res.code == 20000) {
			if (res.data.accessToken) {
				uni.setStorageSync('token', res.data.accessToken);
				userInfo.value.token = res.data.accessToken
				console.log("accessToken 替换");
			}
			userInfo.value.mobile = res.data.mobile
			console.log('20000', res.data);
			userInfo.value.info = res.data;
			uni.setStorageSync('info', JSON.stringify(res.data));
			// 下单
		}
	});
}
/* 支付宝未获取到手机号 */
export function zfbGetSecondPhone(){}
/* button 获取手机号码 apply 1 获取code 2 登陆 3获取电话信息 4 解密电话 */
export function getPhoneNumber() {
	return new Promise((resolve,reject)=>{
		// 登陆
	/* 	my.getPhoneNumber({
			success: (resPhone) => {
				console.log(resPhone, 'getPhoneNumber');
				const params = JSON.parse(resPhone.response)
				
				// 解密电话号码
				initApi.getApllyAuth({
					encryptedData: params.response,
					sign: params.sign
				}).then((resp1) => {
					console.log('成功 解密电话号码', resp1);
					if (resp1.code == 20000) {
						// 手机号
						if (resp1.data.accessToken) {
							uni.setStorageSync('token', resp1.data.accessToken);
							userInfo.value.token = resp1.data.accessToken
							
							console.log("accessToken 替换");
						}
						userInfo.value.mobile = resp1.data.mobile
						console.log('20000', resp1.data);
						userInfo.value.info = resp1.data;
						uni.setStorageSync('info', JSON.stringify(resp1.data));
						return resolve(resp1)
					} else {
						reject(resp1)
						return uni.utils.toast(resp1.message);
					}
				}).catch(err=>{
					console.log(err,'initApi.getApllyAuth');
				});
			},
			fail: (err) => {
				console.log('授权失败 手机号失败', err)
			}
		}) */
		my.getAuthCode({
			scopes: 'auth_base',
			success: res => {
				console.log(res, 'resfirst auth_base');
				const authCode = res.authCode;
				// 在服务端获取用户信息
				initApi.getApplyLogin({
					code: authCode
				}).then(resp => {
					console.log(resp, 'resp', res.code);
					if (resp.code == 20000) {
		
						uni.setStorageSync('token', resp.data.accessToken);
						userInfo.value.token = resp.data.accessToken
						console.log("获取 accessToken --->>");
						my.getPhoneNumber({
							success: (resPhone) => {
								console.log(resPhone, 'getPhoneNumber');
								const params = JSON.parse(resPhone.response)
		
								// 解密电话号码
								initApi.getApllyAuth({
									encryptedData: params.response,
									sign: params.sign
								}).then((resp1) => {
									console.log('成功 解密电话号码', resp1);
									if (resp1.code == 20000) {
										// 手机号
										if (resp1.data.accessToken) {
											uni.setStorageSync('token', resp1.data.accessToken);
											userInfo.value.token = resp1.data.accessToken
											
											console.log("accessToken 替换");
										}
										userInfo.value.mobile = resp1.data.mobile
										console.log('20000', resp1.data);
										userInfo.value.info = resp1.data;
										uni.setStorageSync('info', JSON.stringify(resp1.data));
										return resolve(resp1)
									} else {
										reject(resp1)
										return uni.utils.toast(resp1.message);
									}
								});
							},
							fail: (err) => {
								console.log('授权失败 手机号失败', err)
							}
						})
		
		
		
					} else {
						return uni.utils.toast(resp.message);
					}
				})
			},
			fail: err => {
				console.log('my.getAuthCode 调用失败', err)
			}
		});
		
		
	})


}
export function handleAuthError() {
	console.log('拒绝获取手机号')
}
export function uuIds() {
	var s = [];
	var hexDigits = '0123456789abcdef';
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = '-';

	var uuid = s.join('');
	return uuid;
	// console.log(uuid);
}